package com.ruoyi.washer.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.washer.common.WxWechatTools;
import com.ruoyi.washer.service.impl.XwDeviceServiceImpl;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.washer.domain.XwDevice;
import com.ruoyi.washer.service.IXwDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 *
 * @author hawking
 * @date 2023-12-15
 */
@RestController
@RequestMapping("/washer/devicemanage")
public class XwDeviceController extends BaseController
{
    @Autowired
    private IXwDeviceService xwDeviceService;

    @Autowired
    private WxWechatTools wxWechatTools;

    @Autowired
    private XwDeviceServiceImpl xwDeviceServiceImpl;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('washer:devicemanage:list')")
    @GetMapping("/list")
    public TableDataInfo list(XwDevice xwDevice)
    {
        startPage();
        List<XwDevice> list = xwDeviceService.selectXwDeviceList(xwDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('washer:devicemanage:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XwDevice xwDevice)
    {
        List<XwDevice> list = xwDeviceService.selectXwDeviceList(xwDevice);
        ExcelUtil<XwDevice> util = new ExcelUtil<XwDevice>(XwDevice.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('washer:devicemanage:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return success(xwDeviceService.selectXwDeviceByDeviceId(deviceId));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('washer:devicemanage:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XwDevice xwDevice)
    {


       // System.out.println("========"+xwDevice.getStopGrid01().toString());
/*
        if(xwDevice.getSalesGrid01()!=null){
            xwDevice.setSalesGrid(xwDevice.getSalesGrid01().toString());
        }
        if(xwDevice.getRentGrid01()!=null){
            xwDevice.setRentGrid(xwDevice.getRentGrid01().toString());
        }
        if(xwDevice.getStopGrid01()!=null){
            xwDevice.setStopGrid(xwDevice.getStopGrid01().toString());
        }*/
        return toAjax(xwDeviceService.insertXwDevice(xwDevice));
    }

    /**
     * 修改设备管理
     */
    @PreAuthorize("@ss.hasPermi('washer:devicemanage:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XwDevice xwDevice)
    {
        //System.out.println("========"+xwDevice.getStopGrid01().toString());
        /*if(xwDevice.getSalesGrid01()!=null){
            xwDevice.setSalesGrid(xwDevice.getSalesGrid01().toString());
        }
        if(xwDevice.getRentGrid01()!=null){
            xwDevice.setRentGrid(xwDevice.getRentGrid01().toString());
        }
        if(xwDevice.getStopGrid01()!=null){
            xwDevice.setStopGrid(xwDevice.getStopGrid01().toString());
        }*/
        return toAjax(xwDeviceService.updateXwDevice(xwDevice));
    }

    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('washer:devicemanage:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(xwDeviceService.deleteXwDeviceByDeviceIds(deviceIds));
    }



    @GetMapping("/download/QRcode/{deviceSn}/{type}")
    public void downloadQRcode(HttpServletResponse response,
                                     @PathVariable String deviceSn,
                                     @PathVariable String type){
        String env_version="";
        if(type.equals("1")){//体验版
            env_version="trial";
        }else if(type.equals("2")){//正式版
            env_version="release";
        }else if(type.equals("3")){//开发版
            env_version="develop";
        }
        try{
            wxWechatTools.getQRCode(response,deviceSn,env_version);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /*
获取所有的下载标识
 */
    @GetMapping("/get/all/downloadBatch")
    public AjaxResult getAllDownloadBatch(){
        AjaxResult ajax = new AjaxResult(HttpStatus.SUCCESS, "获取成功");
        ajax.put("data",xwDeviceServiceImpl.getAllDownloadBatch());
        return ajax;
    }
    /*
    下载批量文件
     */
    @GetMapping("/downloadBatch/zipfile")
    public void downloadBatchZipfile(HttpServletResponse response, String downloadBatch) throws IOException
    {
        try{
            byte[] data = xwDeviceServiceImpl.downLoadFile(downloadBatch);
            genCode(response, data);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 生成zip文件
     */
    private void genCode(HttpServletResponse response, byte[] data) throws IOException
    {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }
}
